﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace ListBook
{
    public class Book : IComparable<Book>
    {
        public string BookName { get; }
        public string Publishing { get; }//出版社
        public double Price { get; }//价格
        public string Author { get; }//作者
        public string Publication { get; }  //出版号


        public Book(string bookName, string publishing, double price, string author, string publication)
        {
            BookName = bookName;
            Publishing = publishing;
            Price = price;
            Author = author;
            Publication = publication;
        }
        public override string ToString() => $"书名：{BookName}  作者：{Author} 价格 ：{Price}";

        public virtual string ToString(string format) => ToString(format, null);

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "A":
                    return $"书名：{BookName}   出版社：{Publishing}   价格：{Price}   作者：{Author}   出版号{Publication}";
                case "N":
                    return ToString();
                case "P":
                    return BookName + " " + Price;
                case "B":
                    return BookName;
                case "C":
                    return Publication;
                default:
                    throw new FormatException($"invalid format string {format}");//错误提示
            }
        }



        public int CompareTo([AllowNull] Book other)
        {
            int compare = BookName?.CompareTo(BookName) ?? -1;
            if (compare == 0)
            {
                return Author?.CompareTo(other?.Author) ?? -1;
            }
            return compare;
        }

    }
}
